<?php
	require_once(dirname(__FILE__)."/../core/safeParam.php");
	
	if(isset($_GET['id'])){
		$topid   = $_GET['id'];
	}
	
	$fields  = mysql_list_fields($db['name'], 'pages');
	$columns = mysql_num_fields($fields);
	
	for ($i = 0; $i < $columns; $i++) {
  		$field_array[] = mysql_field_name($fields, $i);
	} //$i = 0; $i < $columns; $i++
	
	if (!in_array('menu', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD menu VARCHAR(255) NOT NULL default 'Main Menu'");
	} //!in_array('menu', $field_array)
	
	if (!in_array('manualmeta', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD manualmeta BIGINT(1) NOT NULL default '0'");
	} //!in_array('manualmeta', $field_array)
	
	if (!in_array('keywords', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD keywords TEXT NOT NULL ");
	} //!in_array('keywords', $field_array)
	
	if (!in_array('window', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD window VARCHAR(255) NOT NULL default ''");
	} //!in_array('window', $field_array)
	
	if (!in_array('link', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD link VARCHAR(255) NOT NULL default ''");
	} //!in_array('link', $field_array)
	
	if (!in_array('status', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD status int(11) NOT NULL default '0'");
	} //!in_array('status', $field_array)
	
	if (!in_array('parent', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD parent VARCHAR(255) NOT NULL default ''");
	} //!in_array('parent', $field_array)
	
	if (!in_array('pos', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD pos TINYINT(11) NOT NULL default '0'");
	} //!in_array('pos', $field_array)
	
	if (!in_array('subcat', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD subcat TEXT NOT NULL");
	} //!in_array('subcat', $field_array)
	
	if (!in_array('hassub', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD hassub INT(1) NOT NULL default '0'");
	} //!in_array('hassub', $field_array)
	
	if (!in_array('cat', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD cat TEXT NOT NULL");
	} //!in_array('cat', $field_array)
	
	if (!in_array('menuitem', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD menuitem INT(1) NOT NULL default '0'");
	} //!in_array('menuitem', $field_array)
	
	if (!in_array('description', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD description TEXT NOT NULL");
	} //!in_array('description', $field_array)
	
	if (!in_array('title', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD title TEXT NOT NULL");
	} //!in_array('title', $field_array)
	
	if (!in_array('linktitle', $field_array)) {
  		$query = $database->query("ALTER TABLE pages ADD linktitle TEXT NOT NULL");
	} //!in_array('linktitle', $field_array)
?>


<div style="margin-left:20px;">
	<h2>Páginas del sitio</h2>
</div>	

<script type="text/javascript">
	function showSubs(showdiv) {
		$("div." + showdiv).slideToggle("slow");
	}

	function showSubsAdd(showdiv) {
		$("div." + showdiv).slideToggle('slow', function(){ $("div." + showdiv).load("pages.php?function=add").slideToggle('slow');});	 
	}

	function showvideoAudio() {
		$("div#videoaudio").fadeIn("slow");
	}
</script>

<?php
	if (isset($updated) && $updated == "1") {
		echo '
		<div style="width:400px; height:35px; border: dashed 1px #CCCCCC;padding:10px;text-align:center" id="success"><h2>Página Actualizada!</h2></div><br>';
	} //$updated == "1"
?>

<?php
	if (!isset($function)){
		$function = "";
	}

	switch ($function) {
		default:
		if ( $_GET['id'] == '')  {
			$editsequence = $edit;
?>
		<h2>
		<input type="button" value="Agregar Página" onClick="parent.location='index.php?edit=pages&function=add'" style="height:35px; width:160px; font-weight:bold">
		&nbsp;&nbsp;&nbsp;&nbsp;
 		<input type="button" value="Agregar Vínculo a una Página" onClick="parent.location='index.php?edit=pages&function=add-link'" style="height:35px; width:240px; font-weight:bold">
 		</h2>
     
     <br />
     <p>Haga clic en una Página de abajo para editarla o acceder a las subpáginas</p>
     <div id="videoaudio" style="background-color:#F7F7F7; display:none">
	     	<table width="100%" border="0">
  		  		<tr>
              <td width="50%">
				  <h4>Cargar archivo de audio</h4>
    				<form method="post" action="../functions/pages.php?function=upload-music" enctype="multipart/form-data" >
    					<fieldset>
	   					<input name="name" id="name"  type="file" /> <input type="submit" value="Cargar archivo" />
						</fieldset>
					</form></td>
    				
    				<td>
    					<h4>Cargar archivo de video</h4>
    					<form method="post" action="../functions/pages.php?function=upload-video" enctype="multipart/form-data" >
    						<fieldset>
								<input name="name" id="name"  type="file" /> <input type="submit" value="Upload File" />
							</fieldset>
						</form>
					</td>
				</tr>
			</table>
	</div>

<h2>Elementos del Menú Principal</h2>
<?php
	echo findPages('Main Menu');
?>
       
<?php
	} //$_GET['id'] == ''
	else {
		$editsequence = $edit;
      //
      // start wysiwyg sequence
      //
      $topid        = $_GET['id'];
      $query        = $database->query("SELECT * FROM pages WHERE id = $topid ");
      
      while ($list = mysql_fetch_array($query)) {
      	$goid             = $list['id'];
        	$gotitle          = $list['title'];
        	$golinktitle      = $list['linktitle'];
        	$gopagename       = $list['pagename'];
        	$godescription    = $list['description'];
        	$gocontent        = $list['content'];
        	$gomenuitem       = $list['menuitem'];
        	$goparent         = $list['parent'];
        	$gomenu           = $list['menu'];
        	$meta_title       = $list['title'];
        	$meta_description = $list['description'];
        	$meta_keywords    = $list['keywords'];
        	$meta_manual      = $list['manualmeta'];
        	
        	if ($meta_manual == '1') {
          	$meta_display = '';
          	$meta_manual  = 'checked="checked"';
        	} //$meta_manual == '1'
        	else {
          	$meta_display = 'display:none';
          	$meta_manual  = '';
        }
      } //$list = mysql_fetch_array($query)
?>	

<script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,ezfilemanager",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen,ezfilemanager",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
  		file_browser_callback : "ezfilemanager",  
  		extended_valid_elements : "iframe[src|width|height|name|align|frameborder|scrolling]",
  		relative_urls : false
  	});

	function ezfilemanager (field_name, url, type, win) {
		//Change the var pluginPath to reflect your installation path
		var PluginPath = "tiny_mce/plugins/ezfilemanager/ezfilemanager.php"; 
      
      if (PluginPath.indexOf("?") < 0) {
      	PluginPath = PluginPath + "?type=" + type;
	   } else {
				PluginPath = PluginPath + "&type=" + type;
   	}
		
		tinyMCE.activeEditor.windowManager.open({ file : PluginPath, title : '', width : 650, height : 440, resizable : "yes", scrollbars : "yes", inline : "yes",  close_previous : "no"}, { window : win, input : field_name});
   	return false;
	}
</script>	


<form method="post" action="../functions/pages.php?function=edit">
<?php
	if (isset($menuitem) && $menuitem == '0') {
		echo '<input type="hidden" name="menuitem" value="0">';
  	} 
?>
	<table width="100%" border="0" class="style2">
		<tr>
			<td colspan="2">
				Nombre (título de la página): 
				<input  name="linktitle" type="text" size="60" value="<?php echo $golinktitle;?>" />
				<input name="date" type="hidden" size="60" value="<?php echo date("Y-m-d");?>" />
				<input name="id" type="hidden" size="60" value="<?php echo $goid;?>" />
				<input name="id" type="hidden" size="60" value="<?php echo $goid;?>" />
				<input type="hidden" name="menu" value="Main Menu" />
			</td>
		</tr>
	
		<tr>
			<td>
				¿Llenar manualmente meta-tags? 
				<input type="checkbox" name="meta_manual" value="1" onclick="$(this).is(':checked') && $('#checked').slideDown('slow') || $('#checked').slideUp('slow');" <?php echo $meta_manual;?> />
				<div id="checked" style="<?php echo $meta_display;?>; height:100px; background-color:#F5F5F5;padding:10px;margin:10px">
					<table width="100%" border="0" cellspacing="0" cellpadding="0">
  						<tr>
    						<td>Título</td>
    						<td><input type="text" name="meta_title"  size="70" value="<?php echo $meta_title;?>" /></td>
  						</tr>
  					
  						<tr>
    						<td>Descripción</td>
    						<td><input type="text" name="meta_description"  size="70" value="<?php echo $meta_description;?>" /></td>
  						</tr>
  					
  						<tr>
    						<td>Palabras clave</td>
    						<td><input type="text" name="meta_keywords"  size="70" value="<?php echo $meta_keywords;?>" /></td>
  						</tr>
					</table>
				</div>
			</td>
		</tr>
	
		<tr>
			<td  colspan="2">
				¿Dónde le gustaría que se encuentre ésta página?
      		<?php
	      		$query3 = $database->query("SELECT * FROM pages WHERE parent = 0 ORDER by pagename");
   	   		echo '<select name="parent">
							<option value="' . $goparent . '" selected  selected="selected">&nbsp;&nbsp;&nbsp;' . $goparent . '</option>	
							<option value="0" >&nbsp;&nbsp;&nbsp;Top Tier Navigation</option>';
	      		
      			while ($row = mysql_fetch_array($query3)) {
        				$listid   = $row['id'];
        				$listname = $row['pagename'];
        				echo "<option value=\"{$listid}\">&nbsp;&nbsp;{$listname}</option>";
      			} //$row = mysql_fetch_array($query3)
      		
      			echo "</select>";
				?>  
		</tr>
	</table>		

	<textarea id="elm1" name="content" rows="45" cols="80" style="width: 100%">		
	<?php
		echo $gocontent;
	?>
	</textarea>
	
	<p align="right"><input type="submit" value="Guardar"/></p>
</form>
<?php
    }
?>

<?php
	break;
	
	case "add":
?>

<script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,ezfilemanager",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen,ezfilemanager",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
  		file_browser_callback : "ezfilemanager",  
  		extended_valid_elements : "iframe[src|width|height|name|align|frameborder|scrolling]",
  		relative_urls : false
	});

	function ezfilemanager (field_name, url, type, win) {
		//Change the var pluginPath to reflect your installation path
		var PluginPath = "tiny_mce/plugins/ezfilemanager/ezfilemanager.php"; 
	
		if (PluginPath.indexOf("?") < 0) {
			PluginPath = PluginPath + "?type=" + type;
   	} else {
   		PluginPath = PluginPath + "&type=" + type;
   	}

		tinyMCE.activeEditor.windowManager.open({
			file : PluginPath,
      	title : '',
       	width : 650, 
       	height : 440,
       	resizable : "yes",
       	scrollbars : "yes",
       	inline : "yes", 
       	close_previous : "no"
   	}, {
     		window : win,
         input : field_name
  		});
	
		return false;
	}
</script>	

<form method="post" action="../functions/pages.php?function=add">  <input type="hidden" name="rid" value="<?php echo ARID;?>" />
	<table width="100%" border="0" class="style2">
		<tr>
    		<td colspan="2">
				Nombre (título de la página)
	  			<input name="linktitle" type="text" size="60" />
				<input name="date" type="hidden" size="60" value="<?php echo date("Y-m-d");?>" /> <input type="hidden" name="menu" value="Main Menu" />
			</td>
  		</tr>

		<tr>
			<tr>
				<td>
					¿Llenar manualmente meta-tags? 
					<input type="checkbox" name="meta_manual" value="1" onclick="$(this).is(':checked') && $('#checked').slideDown('slow') || $('#checked').slideUp('slow');" <?php echo $meta_manual;?> />

					<div id="checked" style="<?php echo $meta_display;?>; height:100px; background-color:#F5F5F5;padding:10px;margin:10px">
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
  							<tr>
    							<td>Título</td>
    							<td>
    								<input type="text" name="meta_title"  size="70" value="<?php echo $meta_title;?>" />
    							</td>
  							</tr>
  							
  							<tr>
    							<td>Descripción</td>
    							<td>
    								<input type="text" name="meta_description"  size="70" value="<?php echo $meta_description;?>" />
    							</td>
  							</tr>
  							
  							<tr>
    							<td>Palabras clave</td>
    							<td><input type="text" name="meta_keywords"  size="70" value="<?php echo $meta_keywords;?>" /></td>
  							</tr>
						</table>
					</div>
				</td>
			</tr>
		
		<tr>
			<td  colspan="2">
				¿Dónde le gustaría que se encuentre ésta página?
				<?php
					$query3 = $database->query("SELECT * FROM pages ORDER by pagename");
    				
    				if (isset($_GET['subid'])) {
      				$subid = '<option value="' . $_GET['subid'] . '" selected="selected">' . $_GET['subpagename'] . '</option>';
      				$selected = '';
    				} else {
      				$selected = 'selected="selected"';
    				}
    				
    				echo '<select name="parent">' . $subid . '<option value="0" ' . $selected . '>Top Tier Navigation</option>';
    				
    				while ($row = mysql_fetch_array($query3)) {
      				$listid   = $row['id'];
      				$listname = $row['pagename'];
      				echo "<option value=\"{$listid}\">&nbsp;&nbsp;&nbsp;{$listname}</option>";
    				} //$row = mysql_fetch_array($query3)
    				
    				echo "</select>";
				?> 
  		</tr>
		
		<tr>
			<td colspan="2"></td>
  		</tr>
	</table>
	
	<textarea id="elm1" name="content" rows="45" cols="80" style="width: 100%"></textarea>
	<br />
	<p align="right"><input type="submit" value="Agregar" style="padding:10px;"/></p>
</form>

<?php
	break;
	
	case "delete":
	$editsequence = $edit;
	$query        = $database->query("SELECT * FROM pages ORDER by title ");
	
	while ($list = mysql_fetch_array($query)) {
		$id          = $list['id'];
      $title       = $list['title'];
      $pagename    = $list['pagename'];
      $description = $list['description'];
      $content     = $list['content'];
      $entry .= '<p style="border-bottom: 1px dashed #CCCCCC">' . $pagename . '<a href="../functions/pages.php?function=delete&id=' . $id . '">(Click here to delete)</a></p>';
    } //$list = mysql_fetch_array($query)
    echo $entry;
    break;
    
    case "add-link":
    $editsequence = $edit;
?>
	<form method="post" action="../functions/pages.php?function=add-link">  <input type="hidden" name="rid" value="<?php echo ARID;?>" />
		<table width="100%" border="0" class="style2">
			<tr>
    			<td colspan="2">
					Nombre del link (títlo de la página)
	  				<input name="linktitle" type="text" size="60" />
					<input name="date" type="hidden" size="60" value="<?php echo date("Y-m-d");?>" />
				</td>
  			</tr>
			
			<tr>
    			<td colspan="2">
					Menu
					<select name="menu">
						<option value="Main Menu" selected="selected">Menu Principal</option>
        				<option value="Secondary Menu">Menu Secundario</option>
         			<option value="No Menu Assigned">No asignar a Menu</option>
        			</select>
				</td>
  			</tr>
			
			<tr>
				<tr>
					<td  colspan="2">
						¿Dónde le gustaría que se encuentre ésta página?
						<?php
							$query3 = $database->query("SELECT * FROM pages ORDER by pagename");
    						
    						if (isset($_GET['subid'])) {
    							$subid = '<option value="' . $_GET['subid'] . '" selected="selected">' . $_GET['subpagename'] . '</option>';
      						$selected = '';
    						} else {
    							$selected = 'selected="selected"';
    						} 
    						
    						echo '<select name="parent"> ' . $subid . ' <option value="0" ' . $selected . '>Top Tier Navigation</option>';
    						
    						while ($row = mysql_fetch_array($query3)) {
      						$listid   = $row['id'];
      						$listname = $row['pagename'];
      						echo "<option value=\"{$listid}\">&nbsp;&nbsp;&nbsp;{$listname}</option>";
    						} //$row = mysql_fetch_array($query3)
    						
    						echo "</select>";
						?> 
  				</tr>
				
				<tr>
					<td colspan="2">
						URL: 
						<input type="text" name="link"  size="60"/>
					</td>
  				</tr>
			</table>
			
			<p align="right"><input type="submit" value="Agregar" style="padding:10px;"/></p>
		</form>
<?php
	break;
	
	case "edit-link":
	$topid = $_GET['id'];
   $query = $database->query("SELECT * FROM pages WHERE id = $topid ");

   while ($list = mysql_fetch_array($query)) {
   	$goid          = $list['id'];
      $gotitle       = $list['title'];
      $golinktitle   = $list['linktitle'];
      $gopagename    = $list['pagename'];
      $godescription = $list['description'];
      $gocontent     = $list['content'];
      $gomenuitem    = $list['menuitem'];
      $goparent      = $list['parent'];
      $golink        = $list['link'];
      $gomenu        = $list['menu'];
   } //$list = mysql_fetch_array($query)
?>

	<form method="post" action="../functions/pages.php?function=edit-link">
		<table width="100%" border="0" class="style2">
			<tr>
    			<td colspan="2">
					Button Name (this is also the page title): 
	  				<input name="linktitle" type="text" size="60" value="<?php echo $golinktitle;?>" />
       			<input name="id" type="hidden" size="60" value="<?php echo $goid;?>" />
					<input name="date" type="hidden" size="60" value="<?php echo date("Y-m-d");?>" />
				</td>
  			</tr>
			
			<tr>
    			<td colspan="2">
					Menu: 
					<select name="menu">
						<option value="<?php echo $gomenu;?>" selected="selected">
							<?php
	    						echo $gomenu;
							?>
						</option>
						<option value="Main Menu">Main Menu</option>
        				<option value="Secondary Menu">Secondary Menu</option>
         			<option value="No Menu Assigned">No Menu Assigned</option>
        			</select>
				</td>
  			</tr>

			<tr>
				<td  colspan="2">
					Select where you would want this page to be located:
					<?php
						$query3 = $database->query("SELECT * FROM pages ORDER by pagename");
    					
    					if (isset($_GET['subid'])) {
      					$subid = '<option value="' . $_GET['subid'] . '" selected="selected">' . $_GET['subpagename'] . '</option>';
      					$selected = '';
    					} else {
      					$selected = 'selected="selected"';
    					}
    					
    					echo '<select name="parent">' . $subid . ' <option value="0" ' . $selected . '>Top Tier Navigation</option>';
    					
    					while ($row = mysql_fetch_array($query3)) {
      					$listid   = $row['id'];
      					$listname = $row['pagename'];
      					echo "<option value=\"{$listid}\">&nbsp;&nbsp;&nbsp;{$listname}</option>";
    					} //$row = mysql_fetch_array($query3)
    					
    					echo "</select>";
					?> 
  			</tr>
			
			<tr>
				<td colspan="2">
					URL: 
					<input type="text" name="link"  size="60" value="<?php echo $golink;?>"/>
				</td>
  			</tr>
		</table>
		
		<p align="right"><input type="submit" value="Guardar"/></p>
	</form>

<?php

    break;
} //$function
?>
